package com.golxen.framework.activity;


import com.alibaba.fastjson.JSON;
import com.golxen.framework.activity.dao.ActivityBussiness;
import com.golxen.framework.activity.dao.entity.ActivityInfo;
import com.golxen.framework.common.BuiltinPlugin;
import com.golxen.framework.common.server.game.GameServer;
import com.golxen.framework.event.condition.dao.entity.ActivityConditionInfo;
import com.golxen.framework.event.condition.dao.entity.UserActivityConditionData;
import com.golxen.framework.event.eGamePlayerEventType;
import com.golxen.framework.manager.TempMgr;
import com.golxen.framework.user.player.GamePlayer;
import com.golxen.utils.LogUtil;
import com.golxen.utils.TimeUtils;
import com.mysql.cj.util.LogUtils;

import java.math.BigInteger;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

public class ActivityMgr extends TempMgr {

	private static List<ActivityInfo> allActivityInfos = new ArrayList<>();
	private static Map<Integer, ActivityInfo> allActivityInfoMap = new HashMap<>();
	private static Map<Integer, Map<Integer, List<ActivityConditionInfo>>> activityConditionInfoTypeMap = new ConcurrentHashMap<>();
	private static Map<Integer, List<ActivityConditionInfo>> activityConditionInfoMap = new ConcurrentHashMap<>();


	@Override
	public boolean stop() {
		return false;
	}

	@Override
	public boolean save() {
		return true;
	}

	@Override
	public boolean reloadConfig() throws Exception {

		return true;
	}

	@Override
	public boolean reloadData() {
		// 载入活动信息
		reloadActivityData();
		return true;
	}

	// 定时刷新每日活动
	public static void taskReload() throws Exception {
		// 载入活动信息
		reloadActivityData();
	}

	public static void reloadActivityData() {
		LogUtil.COMMON.info("reload activity start");
		// 从数据库读出这个服务器的活动信息
		long serverId=BuiltinPlugin.getServerId();
		allActivityInfoMap = ActivityBussiness.getActivityInfoMap(serverId);
		List<Integer> allActivityIdList = new ArrayList<>();

		for (ActivityInfo activityInfo : allActivityInfoMap.values()) {

			LogUtil.COMMON.info("reload activity activityId={} beginShowTime={} beginTime={} endTime={} endShowTime={}", //
					activityInfo.getActivityId(), //
					TimeUtils.getDateFormat(new Date(activityInfo.getBeginShowTime() * 1000)), //
					TimeUtils.getDateFormat(new Date(activityInfo.getBeginTime() * 1000)), //
					TimeUtils.getDateFormat(new Date(activityInfo.getEndTime() * 1000)), //
					TimeUtils.getDateFormat(new Date(activityInfo.getEndShowTime() * 1000)) //
			);
		}

		allActivityInfos.addAll(allActivityInfoMap.values());

		allActivityIdList = allActivityInfos.stream().map(ActivityInfo::getActivityId).collect(Collectors.toList());

		// 从数据库读出给定活动批次id的活动任务条件信息并存入map缓存
		ActivityBussiness.getActivityConditionInfoMap(activityConditionInfoTypeMap, activityConditionInfoMap,
				allActivityIdList);

// TODO:通过各插件管理类加载各活动的活动数据
//		if (XXXPlugin.active()) {
//			XXXActivityMgr.reloadActivityData();
//		}

		LogUtil.COMMON.info("reload activity end");
	}
	public static ActivityInfo getActivityInfo(Integer activityId) {
		return allActivityInfoMap.get(activityId);
	}

	public static Map<Integer, Map<Integer, List<ActivityConditionInfo>>> getActivityConditionInfoTypeMap() {
		return activityConditionInfoTypeMap;
	}

	public static Map<Integer, List<ActivityConditionInfo>> getActivityConditionInfoMap() {
		return activityConditionInfoMap;
	}

	private static boolean isUnFinishActivity(ActivityInfo activityInfo) {
		if (null != activityInfo) {
			long nowTime = System.currentTimeMillis() / 1000;
			return activityInfo.getEndShowTime() > nowTime;
		}
		return false;
	}


	/**
	 * 活动在展示期间
	 */
	public static boolean isActivityInShowTime(ActivityInfo activityInfo) {
		if (null == activityInfo) {
			return false;
		}
		long nowTime = System.currentTimeMillis() / 1000;
		return nowTime >= activityInfo.getBeginShowTime() && nowTime < activityInfo.getEndShowTime();
	}



	/**
	 * 活动进行时间
	 */
	public static boolean isActivityInTime(ActivityInfo activityInfo) {
		if (null == activityInfo) {
			return false;
		}
		long now = System.currentTimeMillis() / 1000;
		return activityInfo.getBeginTime() <= now && activityInfo.getEndTime() > now;
	}



	/**
	 * 活动展示期间
	 */
	public static boolean isActivityInTimeBefore(ActivityInfo activityInfo) {
		if (null == activityInfo) {
			return false;
		}
		long nowTime = System.currentTimeMillis() / 1000;
		return nowTime >= activityInfo.getBeginShowTime() && nowTime < activityInfo.getBeginTime();
	}



	public static UserActivityConditionData initUserActivityConditionData(GamePlayer player,
																		  ActivityConditionInfo conditionInfo) {
		UserActivityConditionData data = new UserActivityConditionData();
		data.setUserId(player.getUserId());
		data.setNickName(player.getUserInfo().getNickName());
		data.setActivityId(conditionInfo.getActivityId());
		data.setConditionId(conditionInfo.getConditionId());
		data.setType(conditionInfo.getType());
		data.setValue(BigInteger.ZERO);
		data.setModifyTime(System.currentTimeMillis() / 1000);
		data.setInsertOption();
		return data;
	}



	public static List<ActivityInfo> getAllActivityInfos() {
		return allActivityInfos;
	}

	public static Map<Integer, ActivityInfo> getShowTimeActivityInfoMap(){
		Map<Integer, ActivityInfo> inTimeActivityMap = new ConcurrentHashMap<>();
		for (ActivityInfo activityInfo : allActivityInfoMap.values()) {
			if (isActivityInShowTime(activityInfo)) {
				inTimeActivityMap.put(activityInfo.getActivityId(), activityInfo);
			}
		}
		return inTimeActivityMap;
	}

	/**
	 * 根据条件编号获取条件信息
	 */
	public static ActivityConditionInfo getActivityConditionInfo(int activityId, int conditionId) {
		List<ActivityConditionInfo> conditionInfoList = activityConditionInfoMap.get(activityId);
		if (null == conditionInfoList) {
			return null;
		}
		for (ActivityConditionInfo activityConditionInfo : conditionInfoList) {
			if (activityConditionInfo.getConditionId() == conditionId) {
				return activityConditionInfo;
			}
		}
		return null;
	}

}
